package com.dlc.shop.api.util;

import org.apache.commons.codec.DecoderException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.*;


/**
 * 邀请码工具类
 */
public class InvitationCodeUtil {

    /**
     * 生成一个6位的邀请码
     * @return 6位邀请码
     */
    public static String generateInviteCode() {
        try {
            UUID uuid = UUID.randomUUID();
            // UUID 转换成字符串
            String uuidStr = uuid.toString();

            // RC4 密钥
            byte[] key = generateKey(16); // 生成16字节的密钥
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "RC4");

            // 初始化 RC4 Cipher
            Cipher cipher = Cipher.getInstance("RC4");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

            // 加密 UUID 字符串
            byte[] encryptedBytes = cipher.doFinal(uuidStr.getBytes());

            // 从加密结果中提取6位作为邀请码
            String code = extractSixDigits(encryptedBytes);
            return code;
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }

    }

    private static byte[] generateKey(int length) {
        SecureRandom secureRandom = new SecureRandom();
        byte[] key = new byte[length];
        secureRandom.nextBytes(key);
        return key;
    }

    private static String extractSixDigits(byte[] encryptedBytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int index = i * 2; // 每个字节可以转换为两个十六进制字符
            if (index >= encryptedBytes.length) {
                break;
            }
            sb.append(String.format("%02X", encryptedBytes[index]));
        }
        return sb.toString().substring(0, 6).toUpperCase();
    }




    private static final char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();


    private static final Random random = new Random();

    public static String generateInviteCode1() {
        StringBuilder inviteCode = new StringBuilder();

        for (int i = 0; i < 6; i++) {
            // 生成0到35之间的随机数
            int index = random.nextInt(ALPHABET.length);
            inviteCode.append(ALPHABET[index]);
        }

        return inviteCode.toString();
    }

    public static void main(String[] args) {
        //long timeStamp1 = System.currentTimeMillis();
        List<String> list = new ArrayList<>();
        Set<String> set = new HashSet<>();
        for(int i = 0;i<100000;i++){
            String code = generateInviteCode1();
            list.add(code);
            set.add(code);
            System.out.println(code);
        }

        System.out.println(list.size());
        System.out.println(set.size());
    }

}
